#include "BlockQueue.hpp"
#include "Task.hpp"
struct ThreadData
{
    /* data */
    BlockQueue<func_t> *bq;
    std::string name;
};

void *consumer(void *arg)
{
    ThreadData *ctd = static_cast<ThreadData *>(arg);

    while (true)
    {
        // sleep(1);
        int data = 0;

        // ctd->bq->Pop(&data);
        // std::cout << "消费者消费一个数据" << data << std::endl;
        func_t f;
        ctd->bq->Pop(&f);

        // 处理数据 
        f();
    }
    return (void *)1;
}

void *producter(void *arg)
{
    ThreadData *ptd = static_cast<ThreadData *>(arg);
    int data = 1;

    while (true)
    {
        sleep(1);
        ptd->bq->Enqueue(PrintLog);
        std::cout << "生产者生产一个数据" << data++ << std::endl;
    }
    return (void *)1;
}

int main()
{
    // BlockQueue<int> *bq = new BlockQueue<int>();
    BlockQueue<func_t> *bq = new BlockQueue<func_t>();

    pthread_t c, p;

    ThreadData ptd = {bq, "生产者"};
    pthread_create(&p, nullptr, producter, (void *)&ptd);

    ThreadData ctd = {bq, "消费者"};
    pthread_create(&c, nullptr, consumer, (void *)&ctd);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    delete bq;
    return 0;
}